import Vue from 'vue'
import VueRouter from 'vue-router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import caches from "util/caches.js";

Vue.use(VueRouter)
const login = () => import("views/login.vue");
const findPwd = () => import("views/findPwd.vue");
const index = () => import("views/index.vue");
const notFound = () => import("views/404.vue");
const main = () => import("views/main.vue");
const dictionary = () => import("views/system/dictionary/list.vue");
const user = () => import("views/system/user/list.vue");
const department = () => import("views/system/department/list.vue");
const permission = () => import("views/system/permission/list.vue");
const role = () => import("views/system/role/list.vue");
const loginrecord = () => import("views/system/loginrecord/list.vue");
const loginfo = () => import("views/system/loginfo/list.vue");
const systemsetting = () => import("views/system/systemsetting/list.vue");
const systeminfo = () => import("views/system/systeminfo/list.vue");

const dishes = () => import("views/dishes/dishes/list.vue");
const orders = () => import("views/order/orders/list.vue");
const currentorder = () => import("views/order/currentorder/list.vue");
const orderitm = () => import("views/order/orderitm/list.vue");
const menu = () => import("views/dishes/menu/list.vue");
const enterprise = () => import("views/crm/enterprise/list.vue");
const historyorder = () => import("views/order/historyorder/list.vue");
const orderperson = () => import("views/order/orderperson/edit.vue");
import setmeal from 'views/business/setmeal/setmeal.vue'
//公共路由
export const constantRouterMap = [{
		path: '/login',
		component: login
	}, {
		path: '/findPwd',
		component: findPwd
	}, {
		path: '/',
		component: index,
		children: [{
			path: 'main',
			component: main,
		}]
	},
	{
		path: '*',
		redirect: "/"
	}
]
//动态路由
export const asyncRouterMap = [{
	path: '/',
	component: index,
	children: [{
		path: 'system/dictionary',
		component: dictionary,
	}, {
		path: 'system/user',
		component: user,
	}, {
		path: 'system/department',
		component: department,
	}, {
		path: 'system/permission',
		component: permission,
	}, {
		path: 'system/role',
		component: role,
	}, {
		path: 'system/loginrecord',
		component: loginrecord,
	}, {
		path: 'system/loginfo',
		component: loginfo,
	}, {
		path: 'system/systemsetting',
		component: systemsetting,
	}, {
		path: 'system/systeminfo',
		component: systeminfo,
	},{
		path: 'dishes/dishes',
		component: dishes,
	}, {
		path: 'order/orders',
		component: orders,
	}, {
		path: 'order/orderitm',
		component: orderitm,
	},{
		path: 'business/setmeal',
		component: setmeal,
	},{
		path: 'dishes/menu',
		component: menu,
	}, {
		path: 'order/currentorder',
		component: currentorder,
	},{
		path: 'crm/enterprise',
		component: enterprise,
	},{
		path: 'order/history',
		component: historyorder,
	},{
		path: 'order/orderperson',
		component: orderperson,
	}]
}];

const router = new VueRouter({
	mode: 'history',
	routes: constantRouterMap
})

//路由的拦截器
router.beforeEach((to, from, next) => {
	//进度条开启
	NProgress.start();
	if (to.path == '/login') {
		caches.token.remove();
	}
	let token = caches.token.get();;
	if (!token && to.path != '/login') {
		if (to.path == '/findPwd') {
			next()
		} else {
			next({
				path: '/login'
			})
		}
	} else {
		next()
	}
})

router.afterEach(transition => {
	//进度条关闭
	NProgress.done();
});

export default router;
